--
-- Author: yawei.zhu
-- Date: 2018-12-18 15:54:51
--
-- PvpChallengeBookRender
--
local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PvpChallengeRewardNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._renders = {}
    for i = 1, 5 do
        local render = IconRender.create(self._ui["icon_"..i])
        render:setTipEnable(true)
        self._renders[i] = render
    end
    self:setContentSize(self._ui.Panel_1:getContentSize())
end

function ClassRef:refresh()
    if not self._data then return end
    local rankIdx = self._data.index
    local data = self._data.data

    local arrDatas = string.split(data, "|")
    local szRank = arrDatas[1]
    local dropId = tonumber(arrDatas[2])

    self._ui.tlt:setString(string.format(L("rankrewardtitle"), szRank))
    for i = 1, 5 do
        self._renders[i]:setVisible(false)
    end

    local itemDatas = UD:parseDropToBagItem(dropId)
    for i, itemData in ipairs(itemDatas) do
        if i > 5 then break end
        self._renders[i]:setData(itemData)
        self._renders[i]:setVisible(true)
    end

    local path = Res.ThroneBgPath .. "team_trophy_icon_3.png"
    if rankIdx <= 2 then
        path = Res.ThroneBgPath .. "team_trophy_icon_".. rankIdx .. ".png"
    end
    self._ui.img:loadTexture(path)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
